#实验4.3

import time

#获取当前时间戳
time.time()

#获取当前时间并以易读方式表示
time.ctime()

#获取当前时间，表示为计算机可处理的时间格式
time.gmtime()

#定义时间的输出效果
t = time.gmtime()
time.strftime("%Y-%m-%d %H:%M:%S",t)

timeStr = '2021-10-26 12:55:20'
time.strptime(timeStr, "%Y-%m-%d %H:%M:%S")

# 返回一个CPU级别的精确时间计数值，单位为秒，这个计数值连续调用差值才有意义
start = time.perf_counter()
end = time.perf_counter()
end - start

#s拟休眠的时间，单位是秒,即等待S秒后出结果
def wait():
    time.sleep(3.3)
    print("PYTHON")
wait()


#花式轮子玩法1
import sys,time
from progressbar import *
total = 100
#基本用法
progress = progressbar()
for i in progress(range(total)):
    time.sleep(0.01)
pbar = progressbar().start()

for i in range(1,100):
    pbar.update(int((i/(total-1)))*100)
    time.sleep(0.01)
pbar.finish()


#ProgressBar中常用的widgets
"Timer"   #计时器
"ETA"     #预计剩余时间
"AbsoluteETA"    #预计结束的绝对时间，耗时很长时使用较方便
"Percentage"     #百分比进度，30%
"SimpleProgress" #计数进度，300/1000
"Counter"     #单纯计数
"Bar"         # “#”号进度条

#ProgressBar例子1
import time
import progressbar
p = progressbar.ProgressBar()
N = 1000
for i in p(range(N)):
    time.sleep(0.01)

#ProgressBar例子2
import sys,time
from progressbar import *
total = 100
#基本用法
progress = ProgressBar()
for i in progress(range(total)):
    time.sleep(0.01)
pbar = ProgressBar().start()

for i in range(1,100):
    pbar.update(int((i/(total-1)))*100)
    time.sleep(0.01)
pbar.finish()

#ProgressBar例子3          #并行显示多个（螺纹）独立进度条
import random
import sys
import threading
import time

import progressbar

output_lock = threading.Lock()


class LineOffsetStreamWrapper:
    UP = '\033[F'
    DOWN = '\033[B'

    def __init__(self, lines=0, stream=sys.stderr):
        self.stream = stream
        self.lines = lines

    def write(self, data):
        with output_lock:
            self.stream.write(self.UP * self.lines)
            self.stream.write(data)
            self.stream.write(self.DOWN * self.lines)
            self.stream.flush()

    def __getattr__(self, name):
        return getattr(self.stream, name)


bars = []
for i in range(5):
    bars.append(
        progressbar.ProgressBar(
            fd=LineOffsetStreamWrapper(i),
            max_value=1000,
        )
    )

    if i:
        print('Reserve a line for the progressbar')


class Worker(threading.Thread):
    def __init__(self, bar):
        super().__init__()
        self.bar = bar

    def run(self):
        for i in range(1000):
            time.sleep(random.random() / 100)
            self.bar.update(i)


for bar in bars:
    Worker(bar).start()

#ProgressBar例子4            #带有宽中文（或其他多字节）字符的条形图
# vim: fileencoding=utf-8
import time
import progressbar


def custom_len(value):
    # These characters take up more space
    characters = {
        '进': 2,
        '度': 2,
    }

    total = 0
    for c in value:
        total += characters.get(c, 1)

    return total


bar = progressbar.ProgressBar(
    widgets=[
        '进度: ',
        progressbar.Bar(),
        ' ',
        progressbar.Counter(format='%(value)02d/%(max_value)d'),
    ],
    len_func=custom_len,
)
for i in bar(range(10)):
    time.sleep(0.1)

#ProgressBar例子5         #带有自定义小部件的栏
import time
import progressbar

widgets=[
    ' [', progressbar.Timer(), '] ',
    progressbar.Bar(),
    ' (', progressbar.ETA(), ') ',
]
for i in progressbar.progressbar(range(20), widgets=widgets):
    time.sleep(0.1)

#ProgressBar例子6         #上下文包装器
import time
import progressbar

with progressbar.ProgressBar(max_value=10) as bar:
    for i in range(10):
        time.sleep(0.1)
        bar.update(i)

#ProgressBar例子7         #将进度条与打印输出相结合
import time
import progressbar

for i in progressbar.progressbar(range(100), redirect_stdout=True):
    print('Some text', i)
    time.sleep(0.1)